//
//  OfficeLeaveDetilVC.swift
//  EduManage
//
//  Created by design02 on 16/12/19.
//  Copyright © 2016年 wsl. All rights reserved.
//


/*
 "SchSerID" : "2549",
 "leave_lesson" : "2",
 "leave_enddate" : "2016-12-13T00:00:00",
 "leave_zpr" : "A,",
 "leave_id" : 430,
 "leave_type" : "病假",
 "leave_status" : 1,
 "leave_right" : 1,
 "leave_image" : "",
 "leave_step" : 322,
 "leave_verifier" : "0",
 "leave_flowid" : "131",
 "leave_endtime" : "1",
 "leave_time" : "1",
 "leave_totaltime" : 1.5,
 "leave_explain" : "头晕，病假 12日下午半天，13日上午半天，合计一天",
 "leave_nowtime" : "2016-12-13T12:19:42",
 "reid" : 1,
 "leave_uid" : "148539",
 "leave_date" : "2016-12-12T00:00:00"
 
 
 */
import UIKit

class OfficeLeaveDetilVC: BaseViewController,UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, MWPhotoBrowserDelegate {

    var dic: JSON!
    var navTitle: String!
    var startLabel, endLabel, totaltime, leaveTypeLabel, leaveReason, leaveImage :UILabel!
    var startLabel1, endLabel1, totaltime1, leaveTypeLabel1, leaveReason1 :UILabel!
    var leaveImageView: UIImageView!
    var tableView: UITableView!
    
    
    var oneImage: PicProgressView!
    var photoCollectionView: UICollectionView!
    let collectionIdentifier = "collectionImgIdentifier"
    
    let imageArray = NSMutableArray()
    var photoArray = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = navTitle + "的请假详情"
        self.view.backgroundColor = UIColor.init(hexString: "#f6f6f6", alpha: 1.0)
        
        setupTableView()
        
        
        NotificationCenter.default.addObserver(self, selector: #selector(OfficeLeaveDetilVC.showBigImage(_:)), name: NSNotification.Name(rawValue: "OfficeLeaveShowBigImage"), object: nil)
    }
   
    
    func setupTableView() {
        tableView = UITableView.init(frame: self.view.bounds)
        tableView.backgroundColor = UIColor.init(hexString: "f6f6f6", alpha: 1)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine
        tableView.separatorColor = UIColor.init(hexString: "f6f6f6", alpha: 1)
        
        self.view.addSubview(tableView)
        
//        请假附件
        let detailLabel = UILabel.init(frame: CGRect(x: 0, y: 2, width: screenW, height: 40))
        detailLabel.text = "    请假附件"
        detailLabel.backgroundColor = UIColor.white
        detailLabel.font = UIFont.systemFont(ofSize: 16.0)
        detailLabel.textAlignment = NSTextAlignment.left

        oneImage = PicProgressView.init(frame: CGRect(x: 15, y: detailLabel.frame.maxY + 10, width: screenW - 30, height: screenW - 30))
        oneImage.isUserInteractionEnabled = true
        let showImage = UITapGestureRecognizer.init(target: self, action: #selector(OfficeLeaveDetilVC.showImage))
        oneImage.addGestureRecognizer(showImage)
        
        photoCollectionView = UICollectionView.init(frame: CGRect(x: 15, y: detailLabel.frame.maxY + 10, width: screenW - 30, height: screenW - 30), collectionViewLayout: photoFlowLayout.init())
        photoCollectionView.backgroundColor = UIColor.white
        photoCollectionView.register(PicCollectionViewCell.self, forCellWithReuseIdentifier: collectionIdentifier)
        photoCollectionView.scrollsToTop = true
        photoCollectionView.delegate = self
        photoCollectionView.dataSource = self
        
        
        let footerView = UIView.init(frame: CGRect(x: 0, y: 0, width: screenW, height: screenW + 32))
        
        footerView.addSubview(detailLabel)
        
        let leave_image = dic["leave_image"].stringValue
        
        let imgArr = (leave_image as NSString).components(separatedBy: "|") as NSArray
        
        if imgArr.count == 0 || leave_image.characters.count == 0 {
            
        }else if imgArr.count == 1 {
            
            let imgUrl = (server + leave_image).replacingOccurrences(of: "../..", with: "")
            
            footerView.addSubview(oneImage)
            
            oneImage.imageUrl = imgUrl
            
        }else {
            footerView.addSubview(photoCollectionView)
            imageArray.addObjects(from: imgArr as [AnyObject])
        }
        
        
        if leave_image.characters.count != 0 {
            self.tableView.tableFooterView = footerView
        }
        
        
        
    }

    
    //MARK: - ui table view delegate and data source
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //获取数据
        let leave_nowtime = dic["leave_nowtime"].stringValue
        let leave_date = dic["leave_date"].stringValue
        let leave_enddate = dic["leave_enddate"].stringValue
        let leave_totaltime = dic["leave_totaltime"].stringValue
        let leave_type = dic["leave_type"].stringValue
        let leave_explain = dic["leave_explain"].stringValue
        
        let leave_image = dic["leave_image"].stringValue
        let leave_time = dic["leave_time"].stringValue
        
        //设置cell 的属性
        let cellIdentifier = "officeLeaveDetailTableViewCellIdentifier"
        
        let cell: OfficeLeaveDetailCell = OfficeLeaveDetailCell.init(style: UITableViewCellStyle.default, reuseIdentifier: cellIdentifier)
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        switch indexPath.row {
        case 0:
            cell.firstLabel.text = "开始时间"
            cell.secondLabel.text = getData(leave_date as NSString)
            break
        case 1:
            cell.firstLabel.text = "结束时间"
            cell.secondLabel.text = getData(leave_enddate as NSString)
            break
        case 2:
            cell.firstLabel.text = "请假时长"
            cell.secondLabel.text = leave_totaltime
            break
        case 3:
            cell.firstLabel.text = "请假原因"
            cell.secondLabel.text = leave_type
            break
        case 4:
            cell.firstLabel.text = "请假详情"
            cell.secondLabel.text = leave_explain

            break
        case 5:
            cell.firstLabel.text = "请假附件"
            cell.secondLabel.text = ""
            break
        default:
            break
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        
        let detailLabel = UILabel.init(frame: CGRect(x: 0, y: 2, width: screenW, height: 40))
        detailLabel.text = "    请假详情"
        detailLabel.backgroundColor = UIColor.white
        detailLabel.font = UIFont.systemFont(ofSize: 16.0)
        detailLabel.textAlignment = NSTextAlignment.left
        detailLabel.backgroundColor = UIColor.white
        
        let leave_explain = dic["leave_explain"].stringValue
        let explainLabel = UILabel.init(frame: CGRect(x: 15, y: detailLabel.frame.maxY + 8, width: screenW - 30, height: getLabelHeight("        " + leave_explain, font: UIFont.systemFont(ofSize: 16.0), wigth: screenW - 30)))
        explainLabel.font = UIFont.systemFont(ofSize: 16.0)
        explainLabel.numberOfLines = 0
        explainLabel.backgroundColor = UIColor.init(hexString: "f6f6f6", alpha: 1)
        explainLabel.text = "        " + leave_explain
        
        let sectionFooterView = UIView.init(frame: CGRect(x: 0, y: 0, width: screenW - 30, height: screenW - 20))
        sectionFooterView.addSubview(detailLabel)
        sectionFooterView.addSubview(explainLabel)
        
        
        return sectionFooterView
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        let leave_explain = dic["leave_explain"].stringValue
        return 55 + getLabelHeight("        " + leave_explain, font: UIFont.systemFont(ofSize: 16.0), wigth: screenW - 30)
    }
    

    //MARK: - ui collection view delefate and datasource 
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell: PicCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: collectionIdentifier, for: indexPath) as! PicCollectionViewCell
        cell.backgroundColor = UIColor.white
        var photo = imageArray.object(at: (indexPath as NSIndexPath).row) as! String
        photo = photo.replacingOccurrences(of: "../..", with: "")
        let imageurl = server + photo
        cell.imageUrl = imageurl
        return cell;
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let imageUrl = dic["leave_image"].stringValue
        let index = String((indexPath as NSIndexPath).row)
        NotificationCenter.default.post(name: Notification.Name(rawValue: "OfficeLeaveShowBigImage"), object: nil, userInfo: ["url" : imageUrl, "index" : index])
        
    }
    
    func showImage() {
        let imageUrl = dic["leave_image"].stringValue
        let index = "0"
        NotificationCenter.default.post(name: Notification.Name(rawValue: "OfficeLeaveShowBigImage"), object: nil, userInfo: ["url" : imageUrl, "index" : index])
    }
    
    
    //MARK: - 显示大图
    func showBigImage(_ notification: Notification) {
        let dic: NSDictionary = (notification as NSNotification).userInfo! as NSDictionary
        let imageUrl = dic.object(forKey: "url")
        let index = UInt(dic.object(forKey: "index") as! String)
        let array = (imageUrl as! String).components(separatedBy: "|")
        let array1 = NSMutableArray()
        array1.addObjects(from: array)
        let n = array1.count
        photoArray = NSMutableArray()
        for i in 0...n-1 {
            var url = array1.object(at: i) as! String
            url = server + url.replacingOccurrences(of: "../..", with: "")
            photoArray.add(MWPhoto.init(url: URL.init(string: url)))
            
        }
        let browser: MWPhotoBrowser = MWPhotoBrowser.init(delegate: self)
        
        browser.displayActionButton = true
        browser.displayNavArrows = false
        browser.displaySelectionButtons = false
        browser.zoomPhotosToFill = true
        browser.alwaysShowControls = false
        browser.enableGrid = true
        browser.startOnGrid = false
        browser.autoPlayOnAppear = false
        
        browser.setCurrentPhotoIndex(index!)
        self.navigationController?.pushViewController(browser, animated: true)
        browser.showNextPhoto(animated: true)
        browser.showPreviousPhoto(animated: true)
    }
    func numberOfPhotos(in photoBrowser: MWPhotoBrowser!) -> UInt {
        return UInt(photoArray.count)
    }
    func photoBrowser(_ photoBrowser: MWPhotoBrowser!, photoAt index: UInt) -> MWPhotoProtocol! {
        
        if index < UInt(photoArray.count) {
            
            return photoArray.object(at: Int(index)) as! MWPhotoProtocol
            
        }
        
        return nil;
    }
    
    //MARk: - 获取 时间
    
    func getData(_ string: NSString) -> String {
        
        let format = DateFormatter()
        let formatString = "yyyy-MM-dd HH:mm:ss"
        format.dateFormat = formatString
        let array: NSArray = string.components(separatedBy: ".") as NSArray
        var time1 = array.object(at: 0) as! String
        time1 = string.replacingOccurrences(of: "T", with: " ")
        let date = format.date(from: time1 )
        format.dateFormat = "yyyy年MM月dd日"
        
        
        let time = format.string(from: date!)
        
        return time
    }
    
    /*
    func setupViews() {
        
        let leave_nowtime = dic["leave_nowtime"].stringValue
        let leave_date = dic["leave_date"].stringValue
        let leave_enddate = dic["leave_enddate"].stringValue
        let leave_totaltime = dic["leave_totaltime"].stringValue
        let leave_type = dic["leave_type"].stringValue
        let leave_explain = dic["leave_explain"].stringValue
        
        let leave_image = dic["leave_image"].stringValue
        let leave_time = dic["leave_time"].stringValue
        
        //后面
        let X1: CGFloat = 10
        let X2: CGFloat = screenW/2
        
        let W: CGFloat = (screenW - X1*2)/2
        let H: CGFloat = 40.0
        
        
        
        startLabel = UILabel.init(frame: CGRect(x: X1, y: 66, width: W, height: H))
        startLabel.text = "开始时间"
        startLabel.font = UIFont.systemFont(ofSize: 16.0)
        startLabel.textAlignment = NSTextAlignment.left
        startLabel.backgroundColor = UIColor.white
        
        startLabel1 = UILabel.init(frame: CGRect(x: X2, y: 66, width: W, height: H))
        startLabel1.text = getData(leave_date as NSString)
        
        startLabel1.font = UIFont.systemFont(ofSize: 14.0)
        startLabel1.textAlignment = NSTextAlignment.right
        startLabel1.backgroundColor = UIColor.white
        
        endLabel = UILabel.init(frame: CGRect(x: X1, y: startLabel.frame.maxY+1, width: W, height: H))
        endLabel.text = "结束时间"
        endLabel.font = UIFont.systemFont(ofSize: 16.0)
        endLabel.textAlignment = NSTextAlignment.left
        endLabel.backgroundColor = UIColor.white
        
        endLabel1 = UILabel.init(frame: CGRect(x: X2, y: startLabel.frame.maxY+1, width: W, height: H))
        endLabel1.text = getData(leave_enddate as NSString)
        endLabel1.font = UIFont.systemFont(ofSize: 14.0)
        endLabel1.textAlignment = NSTextAlignment.right
        endLabel1.backgroundColor = UIColor.white
        
        
        totaltime = UILabel.init(frame: CGRect(x: X1, y: endLabel.frame.maxY+1, width: W, height: H))
        totaltime.text = "请假时长"
        totaltime.font = UIFont.systemFont(ofSize: 16.0)
        totaltime.textAlignment = NSTextAlignment.left
        totaltime.backgroundColor = UIColor.white
        
        totaltime1 = UILabel.init(frame: CGRect(x: X2, y: endLabel.frame.maxY+1, width: W, height: H))
        totaltime1.text = leave_totaltime
        totaltime1.font = UIFont.systemFont(ofSize: 14.0)
        totaltime1.textAlignment = NSTextAlignment.right
        totaltime1.backgroundColor = UIColor.white
        
        leaveTypeLabel = UILabel.init(frame: CGRect(x: X1, y: totaltime.frame.maxY+1, width: W, height: H))
        leaveTypeLabel.text = "请假原因"
        leaveTypeLabel.font = UIFont.systemFont(ofSize: 16.0)
        leaveTypeLabel.textAlignment = NSTextAlignment.left
        leaveTypeLabel.backgroundColor = UIColor.white
        
        leaveTypeLabel1 = UILabel.init(frame: CGRect(x: X2, y: totaltime.frame.maxY+1, width: W, height: H))
        leaveTypeLabel1.text = leave_type
        leaveTypeLabel1.font = UIFont.systemFont(ofSize: 14.0)
        leaveTypeLabel1.textAlignment = NSTextAlignment.right
        leaveTypeLabel1.backgroundColor = UIColor.white
        
        leaveReason  = UILabel.init(frame: CGRect(x: X1, y: leaveTypeLabel.frame.maxY+1, width: W, height: H))
        leaveReason.text = "请假详情"
        leaveReason.font = UIFont.systemFont(ofSize: 16.0)
        leaveReason.textAlignment = NSTextAlignment.left
        leaveReason.backgroundColor = UIColor.white
        
        leaveReason1 = UILabel.init(frame: CGRect(x: X2, y: leaveTypeLabel.frame.maxY+1, width: W, height: H))
        leaveReason1.text = leave_explain
        leaveReason1.font = UIFont.systemFont(ofSize: 14.0)
        leaveReason1.textAlignment = NSTextAlignment.right
        leaveReason1.backgroundColor = UIColor.white
        leaveReason1.numberOfLines = 0
        
        leaveImage  = UILabel.init(frame: CGRect(x: X1, y: leaveReason.frame.maxY+1, width: W*2, height: H))
        leaveImage.text = "附件详情"
        leaveImage.font = UIFont.systemFont(ofSize: 16.0)
        leaveImage.textAlignment = NSTextAlignment.left
        leaveImage.backgroundColor = UIColor.white
        
        let imgUrl: URL = NSURL.init(string: server + leave_image) as! URL
        leaveImageView = UIImageView.init(frame: CGRect(x: X1, y: leaveImage.frame.maxY+1, width: W*2, height: view.frame.maxY-H*6-15-64-10-50))
        leaveImageView.backgroundColor = UIColor.init(hexString: "#f6f6f6", alpha: 1.0)
        leaveImageView.sd_setImage(with: imgUrl, placeholderImage: nil)
        //        leaveImageView.layer.shadowColor = UIColor.lightGray.cgColor
        //        leaveImageView.layer.shadowOffset = CGSize(width: 0, height: 2)
        //        leaveImageView.layer.shadowRadius = 2
        //        leaveImageView.layer.shadowOpacity = 0.8
        
        view.addSubview(startLabel)
        view.addSubview(endLabel)
        view.addSubview(totaltime)
        view.addSubview(leaveTypeLabel)
        view.addSubview(leaveReason)
        
        view.addSubview(startLabel1)
        view.addSubview(endLabel1)
        view.addSubview(totaltime1)
        view.addSubview(leaveTypeLabel1)
        view.addSubview(leaveReason1)
        
        view.addSubview(leaveImage)
        view.addSubview(leaveImageView)
    }
    */
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
